package com.neusoft.yuejian12.yuejian_12.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.neusoft.yuejian12.yuejian_12.R;
import com.neusoft.yuejian12.yuejian_12.base.RetrofitCallback;
import com.neusoft.yuejian12.yuejian_12.javabean.UserBean;
import com.neusoft.yuejian12.yuejian_12.listener.RetrofitListener;
import com.neusoft.yuejian12.yuejian_12.model.UserModel;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class LoginActivity extends AppCompatActivity implements View.OnClickListener,RetrofitListener<UserBean>{

    private Button regBtn;
    private EditText userPhone;
    private EditText userPass;
    private CheckBox rememberme;
    private Button loginBtn;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    //  int loginstate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        initEvents();
    }

    private void initViews() {
        userPhone = (EditText) findViewById(R.id.uphone);
        userPass = (EditText) findViewById(R.id.upass);
        rememberme = (CheckBox) findViewById(R.id.ckx_rember);
        loginBtn = (Button) findViewById(R.id.loginbtn);
        regBtn = (Button) findViewById(R.id.regbtnl);
        rememberme = (CheckBox) findViewById(R.id.ckx_rember);
        sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
        readstate();
    }

    private void initEvents() {
        regBtn.setOnClickListener(this);
        loginBtn.setOnClickListener(this);
    }

    private void readstate() {
        String sputel = sharedPreferences.getString("u_name", null);
        String sppwd = sharedPreferences.getString("u_pass", null);
//        int flag = sharedPreferences.getInt("loginflag", 0);
//        if (1 == flag) {
//            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
//            startActivity(intent);
//            editor.putInt("loginflag", loginstate);
//            editor.commit();
//            finish();
//        }
        if(!sputel.equals("")&&!sppwd.equals("")){
            Boolean bremember=sharedPreferences.getBoolean("remember", false);
            if (bremember){
                userPhone.setText(sputel);
                userPass.setText(sppwd);
                rememberme.setChecked(bremember);

            }
            //  login(sputel,sppwd);

        }

    }


    public void saveUser(int u_id) {
        editor.putString("u_name", userPhone.getText().toString());
        editor.putInt("u_id", u_id);
        //  editor.putInt("loginflag", loginstate);
        editor.putString("u_pass", userPass.getText().toString());
        Boolean rem = rememberme.isChecked();
        editor.putBoolean("remember", rem);
        editor.commit();

    }

    @Override
    public void onClick(View view) {
        String phone = userPhone.getText().toString().trim();
        String pass = userPass.getText().toString().trim();
        switch (view.getId()) {
            case R.id.loginbtn:
                login(phone, pass);
                break;
            case R.id.regbtnl:
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this, RegisterActivity.class);
                startActivityForResult(intent, 1);
                break;
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) {
            Bundle bundle = data.getExtras();
            String UPhone = bundle.getString("userphone");
            userPhone.setText(UPhone);
        }

    }

    public void login(String phone, String pass) {
        UserModel userModel = new UserModel();
        userModel.userLogin(userPhone.getText().toString(),userPass.getText().toString(),this);
    }


    @Override
    public void onSuccess(UserBean userBean) {
        if (!"0".equals(userBean.getU_id())) {
            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
            saveUser(userBean.getU_id());
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            finish();

      }

    }

    @Override
    public void onFail() {
        Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
    }
}






